using UnityEngine;
namespace MyTools{

public static class MyMath{
    public static float AngleJudge(Vector2 current, Vector2 anchor){//2D，以anchor为锚点的旋转量 向右为0度
        Vector2 currentDir = (current - anchor).normalized;  // <span style="white-space:pre">	</span>//当前方向
        float currentDot = Vector2.Dot(Vector2.right, currentDir);
    
        float currentAngle = current.y < anchor.y
            ? Mathf.Acos(currentDot) * Mathf.Rad2Deg
            : -Mathf.Acos(currentDot) * Mathf.Rad2Deg;
        if (currentAngle>0){
            currentAngle-=360;
        }
        return -currentAngle;
    }
    
    public static Vector2 GetGlobalFaceVec2D(GameObject go){//基于(1,0)向量
        float angleRadians = (go.transform.eulerAngles.z-90) * Mathf.Deg2Rad;

        // 计算向量的X和Y分量
        float x = Mathf.Cos(angleRadians);
        float y = Mathf.Sin(angleRadians);

        // 返回单位向量
        return new Vector2(x, y);
    }

    public static float GetGlobalFaceAngle2D(GameObject go){
        return go.transform.eulerAngles.z;
    }
    public static float GetGlobalFaceAngle2D(Vector2 vec){
        return AngleJudge(vec, new Vector2(1,0));
    }


    public static Vector3 GetAFaceB(GameObject a, GameObject b){
        return (b.transform.position - a.transform.position).normalized;
    }
    public static Vector3 GetAFaceB(Vector3 a, Vector3 b){
        return (b - a).normalized;
    }


    // a向量在b上的投影 不会归一化
    public static Vector3 GetProjection(Vector3 a, Vector3 b){
        return Vector3.Dot(a,b)/Vector3.Dot(b,b)*b;
    }
    // a向量在b上的正交部分
    public static Vector3 GetOrthogonal(Vector3 a, Vector3 b){
        return a - GetProjection(a,b);
    }


    public static float Distance(GameObject a, GameObject b){
        return Vector3.Distance(a.transform.position, b.transform.position);
    }
    // b 是左下角, c是右上角
    public static void FitV2AinV2BAndV2C(ref Vector2 a, ref Vector2 b, ref Vector2 c){
        a.x = Mathf.Clamp(a.x, b.x, c.x);
        a.y = Mathf.Clamp(a.y, b.y, c.y);
    }


    // p1 是线段A起点, p2线段A终点 p3线段B起点 p4线段B终点 求线段是否相交,若是,则intersection为交点
    public static bool LineSegmentIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, out Vector2 intersection)
    {
        intersection = Vector2.zero;

        float A1 = p2.y - p1.y;
        float B1 = p1.x - p2.x;
        float C1 = A1 * p1.x + B1 * p1.y;

        float A2 = p4.y - p3.y;
        float B2 = p3.x - p4.x;
        float C2 = A2 * p3.x + B2 * p3.y;

        float denominator = A1 * B2 - A2 * B1;

        if (Mathf.Abs(denominator) < Mathf.Epsilon)
        {
            return false; // 平行或共线
        }

        float intersectX = (B2 * C1 - B1 * C2) / denominator;
        float intersectY = (A1 * C2 - A2 * C1) / denominator;
        Vector2 intersectPoint = new Vector2(intersectX, intersectY);

        if (IsPointOnLineSegment(p1, p2, intersectPoint) && IsPointOnLineSegment(p3, p4, intersectPoint))
        {
            intersection = intersectPoint;
            return true;
        }

        return false;
    }
    public static bool IsPointOnLineSegment(Vector2 p1, Vector2 p2, Vector2 point)
    {
        return point.x >= Mathf.Min(p1.x, p2.x) && point.x <= Mathf.Max(p1.x, p2.x) &&
               point.y >= Mathf.Min(p1.y, p2.y) && point.y <= Mathf.Max(p1.y, p2.y);
    }


    public static Vector3 GenerateRandomPointOnCircle(Vector3 origin, float radius, string plane) 
    { 
        float angle = Random.Range(0f, 2 * Mathf.PI); 
        // Random angle in radians 
        float x = radius * Mathf.Cos(angle); 
        float y = radius * Mathf.Sin(angle); 
        switch (plane.ToLower()) { 
            case "xy": return new Vector3(origin.x + x, origin.y + y, origin.z); 
            case "xz": return new Vector3(origin.x + x, origin.y, origin.z + y); 
            case "yz": return new Vector3(origin.x, origin.y + x, origin.z + y); 
            default: Debug.LogError("Invalid plane specified. Choose 'xy', 'xz', or 'yz'."); 
            return Vector3.zero; // Return zero vector if invalid plane is specified 
        }
    }
    public static Vector3 GenerateRandomPointInCircle(Vector3 origin, float radius, string plane) 
    { 
        float angle = Random.Range(0f, 2 * Mathf.PI); 
        // Random angle in radians 
        radius = Random.Range(0f, radius);
        float x = radius * Mathf.Cos(angle); 
        float y = radius * Mathf.Sin(angle); 
        switch (plane.ToLower()) { 
            case "xy": return new Vector3(origin.x + x, origin.y + y, origin.z); 
            case "xz": return new Vector3(origin.x + x, origin.y, origin.z + y); 
            case "yz": return new Vector3(origin.x, origin.y + x, origin.z + y); 
            default: Debug.LogError("Invalid plane specified. Choose 'xy', 'xz', or 'yz'."); 
            return Vector3.zero; // Return zero vector if invalid plane is specified 
        }
    }
    public static Vector3 RandomVector3(string plain=""){
        switch (plain.ToLower()) {
            case "xy": return new Vector3(Random.Range(-1f,1f),Random.Range(-1f,1f),0).normalized;
            case "xz": return new Vector3(Random.Range(-1f,1f),0,Random.Range(-1f,1f)).normalized;
            case "yz": return new Vector3(0,Random.Range(-1f,1f),Random.Range(-1f,1f)).normalized;
        }
        return new Vector3(Random.Range(-1f,1f),Random.Range(-1f,1f),Random.Range(-1f,1f)).normalized;
    }
}


}